<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="安全检查员姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="安全检查员姓名"></el-input>
    </el-form-item>
    <el-form-item label="安全检查员电话号码" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="安全检查员电话号码"></el-input>
    </el-form-item>
    <el-form-item label="安全检查员照片" prop="photo">
      <el-input v-model="dataForm.photo" placeholder="安全检查员照片"></el-input>
    </el-form-item>
    <el-form-item label="负责检查的楼栋id" prop="buildingId">
      <el-input v-model="dataForm.buildingId" placeholder="负责检查的楼栋id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          phone: '',
          photo: '',
          buildingId: ''
        },
        dataRule: {
          name: [
            { required: true, message: '安全检查员姓名不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '安全检查员电话号码不能为空', trigger: 'blur' }
          ],
          photo: [
            { required: true, message: '安全检查员照片不能为空', trigger: 'blur' }
          ],
          buildingId: [
            { required: true, message: '负责检查的楼栋id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$request({
              url: `/inspection/inspector/info/${this.dataForm.id}`,
              method: 'get',
              params: {}
            }).then((res) => {
              if (res && res.code === 200) {
                this.dataForm.name = res.inspector.name
                this.dataForm.phone = res.inspector.phone
                this.dataForm.photo = res.inspector.photo
                this.dataForm.buildingId = res.inspector.buildingId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$request({
              url: `/inspection/inspector/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'photo': this.dataForm.photo,
                'buildingId': this.dataForm.buildingId
              }
            }).then((res) => {
              if (res && res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
